package string;

public class NumberOfValidWordsInASentence2047 {
    public int countValidWords(String sentence) {
        int count = 0;
        String[] words = sentence.split(" ");
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            if(word.equals("")){
                continue;
            }
            boolean add = true;
            int cc = 0;
            for (int j = 0; j < word.length(); j++) {
                char c = word.charAt(j);
                if (c - '0' >= 0 && c - '0' <= 9) {
                    add = false;
                    break;
                }
                if (word.charAt(j) == '-') {
                    if (cc != 0 || j == 0 || j == word.length() - 1 || !Character.isLetter(word.charAt(j - 1)) || !Character.isLetter(word.charAt(j + 1))) {
                        add = false;
                        break;
                    }
                    cc++;
                }
                if (c=='!'||c==','||c=='.'){
                    if(j!=word.length()-1){
                        add=false;
                        break;
                    }
                }

            }

            if (add) {
                count++;
            }
        }
        return count;
    }
}
